{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6b3a6ae7-59e0-4498-814e-a93e1d634cdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "%use fuel(2.3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "75d3abee-04d6-4fd0-a18f-38063fb0afa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import java.math.BigInteger"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ab39a624-cec1-4102-b23d-640c312b72dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "val envs = java.io.File(\"../../.env\")\n",
    "    .readLines()\n",
    "    .map {\n",
    "        it.split(\"=\")[0] to it.split(\"=\")[1].trim('\"')\n",
    "    }.toMap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "575b8a31-6e1c-4edc-98f8-72c52992797b",
   "metadata": {},
   "outputs": [],
   "source": [
    "val session = envs.get(\"AOC_SESSION\")\n",
    "val year = 2021\n",
    "val day = 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dd799fac-9c82-4c5a-b308-60691a01a866",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun getInput(year: Int, day: Int, session: String): String {\n",
    "    val (_, _, result) = \"https://adventofcode.com/$year/day/$day/input\"\n",
    "    .httpGet()\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cac8a111-9146-49ea-8989-c6392869eaaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun submitAnswer(year: Int, day: Int, session: String, level: Int, answer: String): String {\n",
    "    val (_, _, result) = Fuel\n",
    "    .post(\n",
    "        \"https://adventofcode.com/$year/day/$day/answer\", \n",
    "        parameters = listOf(\"level\" to level, \"answer\" to answer))\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a064468c-9c51-4307-ba19-e3ff83c6c4ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "val sample = \"\"\"be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe\n",
    "edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc\n",
    "fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg\n",
    "fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb\n",
    "aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea\n",
    "fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb\n",
    "dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe\n",
    "bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef\n",
    "egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb\n",
    "gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "878489a4-5e1e-4c04-a209-650185a8de67",
   "metadata": {},
   "outputs": [],
   "source": [
    "val input = getInput(year, day, session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f80e521a-bf11-4b36-85af-14f9111d262e",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun cross(s: String, t: String): Int {\n",
    "    return s.toSet().intersect(t.toSet()).size\n",
    "}\n",
    "\n",
    "fun norm(s: String): String {\n",
    "    val t = s.toMutableList()\n",
    "    t.sort()\n",
    "    return t.joinToString(\"\")\n",
    "}\n",
    "\n",
    "fun decode(line: String): List<Int> {\n",
    "    val (dict, msg) = line.split('|')\n",
    "    val lw = dict\n",
    "        .split(' ')\n",
    "        .filter { it.length > 0 }\n",
    "    val code = Array(10) { \"\" }\n",
    "    \n",
    "    for (word in lw) {\n",
    "        when (word.length) {\n",
    "            2 -> code[1] = norm(word)\n",
    "            3 -> code[7] = norm(word)\n",
    "            4 -> code[4] = norm(word)\n",
    "            7 -> code[8] = norm(word)\n",
    "            else -> {}\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    for (word in lw) {\n",
    "        if (word.length == 5 && cross(word, code[4]) == 2) {\n",
    "            code[2] = norm(word)\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    for (word in lw) {\n",
    "        if (word.length == 5) {\n",
    "            when (cross(word, code[2])) {\n",
    "                3 -> code[5] = norm(word)\n",
    "                4 -> code[3] = norm(word)\n",
    "                else -> {}\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    for (word in lw) {\n",
    "        if (word.length == 6) {\n",
    "            if (cross(word, code[3]) == 5) {\n",
    "                code[9] = norm(word)\n",
    "            } else if (cross(word, code[1]) == 2) {\n",
    "                code[0] = norm(word)\n",
    "            } else {\n",
    "                code[6] = norm(word)\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    val mapping = (0 until 10).map {\n",
    "        code[it] to it\n",
    "    }.toMap()\n",
    "    \n",
    "    return msg\n",
    "        .split(' ')\n",
    "        .filter { it.length > 0 }\n",
    "        .map { mapping.getOrDefault(norm(it), 0) }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "07df67e5-7a24-42e8-a6c4-f555675939df",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partOne(input: String): String {\n",
    "    val lines = input.split('\\n').filter {it.length > 0}\n",
    "    return lines.map {\n",
    "        decode(it).count {x -> x in setOf(1, 4, 7, 8)}\n",
    "    }.sum().toString()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "eaa273a8-a166-44e0-9023-2449fecbbb94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partOne(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2ad99e07-5a96-40c4-873c-e72dd7c5f499",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "519"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partOneAns = partOne(input)\n",
    "partOneAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23679ce7-8258-41ae-9813-dff5745b482c",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 1, partOneAns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "528387cf-abb5-4da2-9fd3-9bbf865444a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partTwo(input: String): String {\n",
    "    val lines = input.split('\\n').filter {it.length > 0}\n",
    "    return lines.map {\n",
    "        decode(it)\n",
    "            .map { it.toString() }\n",
    "            .joinToString(\"\")\n",
    "            .toInt()\n",
    "    }.sum().toString()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b12a86b5-8d6f-4a6c-851a-af1d84ae168d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "61229"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partTwo(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "62a5e94e-550f-42fe-9597-32e6f87a5b1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1027483"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partTwoAns = partTwo(input)\n",
    "partTwoAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12e74919-3d56-4d88-9c51-bcb437ad3cf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 2, partTwoAns)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.6.20-dev-5432"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
